#pragma once
#include<vector>

namespace bit
{
	template<class T>
	class myless
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T>
	class mygreater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T,class Container=vector<T>,class Compare=myless<T>>
	class priority_queue
	{
	public:
		priority_queue() = default;

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_co.push_back(*first);
				++first;
			}

			for (int i = 0; i < (_co.size() - 1 - 1) / 2; ++i)
			{
				Adjust_down(i);
			}

		}

		void Adjust_down(size_t parent)
		{
			size_t child = parent * 2 + 1;

			while (child < _co.size())
			{
				if (child + 1 < _co.size() && con(_co[child] , _co[child + 1]))
				{
					child = child + 1;
				}

				if (con(_co[parent] , _co[child]))
				{
					swap(_co[parent], _co[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void Adjust_up(size_t child)
		{
			size_t parent = (child - 1) / 2;

			while (child > 0)
			{
				if ( con(_co[parent], _co[child]))
				{
					swap(_co[child], _co[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		T& top ()
		{
			return _co[0];
		}

		const T& top()const
		{
			return _co[0];
		}

		void pop()
		{
			swap(_co[0], _co[_co.size() - 1]);
			_co.pop_back();
			Adjust_down(0);
		}

		void push(const T& x)
		{
			_co.push_back(x);
			Adjust_up(_co.size() - 1);
		}

		size_t size() const
		{
			return _co.size();
		}

		bool empty() const
		{
			return _co.empty();
		}

	private:
		Container _co;

		Compare con;
	};
};